package com.sx.config;

import com.sx.scheduler.ClassRoomScheduler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import javax.annotation.Resource;
import java.util.List;

/**
 * 根据配置项动态配置定时任务
 */
@Configuration
public class DynamicSchedulerConfig implements SchedulingConfigurer {

    @Resource
    private SchedulerConfig schedulerConfig;

    @Resource
    private ClassRoomScheduler classRoomScheduler;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        List<String> cronExpressions = schedulerConfig.getCronExpressions();
        for (String cronExpression : cronExpressions) {
            taskRegistrar.addCronTask(classRoomScheduler::monitorClassRooms, cronExpression);
        }
    }
}
